﻿using System;
using System.IO;
using SVTTranslateTextEngine.Core;
using SVTTranslateTextEngine.Properties;
using SVTTranslateTextEngine.TTChecker;
using SVTTranslateTextEngine.TTInputParams;
using SVTTranslateTextEngine.TTResults;

namespace SVTTranslateTextEngine
{
    /// <summary>
    /// This class to handle the translation function how to do 
    /// </summary>
    public class TranslateManger
    {
        private const string ERROR_MSG_FAIL = "Translation is fail!\r\n";
        protected FileAccessHelper file_access_helper = new FileAccessHelper();
        protected FileChecker file_checker = new FileChecker();
        protected LanguageChecker language_checker = new LanguageChecker();
        protected TextChecker text_checker = new TextChecker();

        /// <summary>
        /// Do the translation by the engine 
        /// </summary>
        /// <param name="szFromLang">source language code </param>
        /// <param name="szToLang">target language code </param>
        /// <param name="szFromText">source context which will be translated</param>
        /// <returns>target context as the string</returns>
        protected string Translate(ref string szFromLang, string szToLang, string szFromText)
        {
            if (string.IsNullOrEmpty(szFromLang))
                return new TranslatorByGoogleHttp().GetTranslationsByLongSentence(szFromText, "",
                                                                                  szToLang);
            else
                return new TranslatorByGoogleHttp().GetTranslationsByLongSentence(szFromText,
                                                                                  szFromLang,
                                                                                  szToLang);
        }

        /// <summary>
        /// Do the translation and return the text on soap object . the data transfer by the http model 
        /// </summary>
        /// <param name="input">text input paramer </param>
        /// <returns>result as string which in the soap object </returns>
        public ToTextResult TranslateToText(TextInputParam input)
        {
            var result = new ToTextResult
                             {
                                 szToLang = input.szToLang,
                                 szFromLang = input.szFromLang,
                                 szErrorMessage = ""
                             };
            result.szErrorMessage += language_checker.CheckIsSupported(input);
            result.szErrorMessage += text_checker.CheckTextIsCanTranslate(input.szFromText);
            if (result.szErrorMessage.Length > 0)
                return result;
            string translateText = Translate(ref input.szFromLang, input.szToLang, input.szFromText);
            if (string.IsNullOrEmpty(translateText))
                result.szErrorMessage = ERROR_MSG_FAIL;
            else
            {
                result.szFromLang = input.szFromLang;
                result.szToText = translateText;
            }
            return result;
        }


        /// <summary>
        /// get share folder of the temp file for the translation .
        /// </summary>
        /// <returns>the share folder's full path as string </returns>
        protected string GetTranslateTextShareDirPath()
        {
            var reuslt = "";
            var shareDirPath = "\\\\" + file_access_helper.GetIPAddress() + "\\" +
                                      Settings.Default.TranslateTextShareDirPath;
            if(Directory.Exists(shareDirPath))
            {
                reuslt = shareDirPath;
            }
            return reuslt;

        }

        /// <summary>
        /// get share folder full path which the source context file are existing  .
        /// </summary>
        /// <returns>the share folder's full path as string </returns>
        public string GetSourceContextShareDirPath()
        {
            var reslut = "";
            var shareDirPath = GetTranslateTextShareDirPath();
            if(!string.IsNullOrEmpty(shareDirPath) && Directory.Exists(shareDirPath))
            {
                shareDirPath += "\\SourceFiles";
                if (!Directory.Exists(shareDirPath)) Directory.CreateDirectory(shareDirPath);
                reslut = shareDirPath;
            }
            return reslut;
        }

        /// <summary>
        /// Do the translation and return the result by file transfer model 
        /// </summary>
        /// <param name="input">the file input parameter </param>
        /// <returns>the result is contain the file information  </returns>
        public ToFileResult TranslateToFile(TextInputParam input)
        {
            ToFileResult result = null;
            var errMessage = language_checker.CheckIsSupported(input);
            errMessage += text_checker.CheckTextIsCanTranslate(input.szFromText);
            if (string.IsNullOrEmpty(errMessage))
            {
                result = CreateToFileResult(input.szFromLang, input.szToLang, errMessage, input.szFromText);
            }
            return result;
        }

        /// <summary>
        /// Create the object of ToFileResult 
        /// </summary>
        /// <param name="szFromLang">source language's code </param>
        /// <param name="szToLang">target language's code </param>
        /// <param name="szErrorMessage">error message by the translation process running </param>
        /// <param name="szFromText">source context </param>
        /// <returns>the result is contain the target context file information </returns>
        protected ToFileResult CreateToFileResult(string szFromLang,string szToLang, string szErrorMessage,string szFromText)
        {
            var result = new ToFileResult
            {
                szToLang = szToLang,
                szFromLang = szFromLang,
                szErrorMessage = szErrorMessage
            };
            if (result.szErrorMessage.Length > 0)
                return result;

            string translateText = Translate(ref szFromLang, szToLang, szFromText);
            if (string.IsNullOrEmpty(translateText))
                result.szErrorMessage = ERROR_MSG_FAIL;
            else
            {
                result.szFromLang = szFromLang;

                var shareDirPath = GetTranslateTextShareDirPath();
                if (!Directory.Exists(shareDirPath))
                    Directory.CreateDirectory(shareDirPath);
                string filePath = shareDirPath + "\\" + Guid.NewGuid() + ".txt";
                file_access_helper.WriteTextContext(translateText, filePath);
                result.szToFilename = filePath;
            }
            return result;
        }

        /// <summary>
        /// Do the translate and return the target context file model 
        /// </summary>
        /// <param name="input">the file input parameter </param>
        /// <returns>the result is contain the target context file information </returns>
        public ToFileResult TranslateToFile(FileInputParam input)
        {
            ToFileResult result = null;
            var errMessage= language_checker.CheckIsSupported(input);
            errMessage += file_checker.CheckFileIsCanTranslate(input);
            if(string.IsNullOrEmpty(errMessage))
            {
                string sourceContext = file_access_helper.ReadTextContext(input.szFromFilename);
                result = CreateToFileResult(input.szFromLang, input.szToLang, errMessage, sourceContext);
            }
            return result;
        }
    }
}